﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace ManageBanking.Models
{
    public class ChequeModels
    {
        OnlineBankingDataContext olbk = new OnlineBankingDataContext();
           
        public bool AddCheque(int idusername,string idaccount,string name, string Address, decimal money, DateTime Date)
        {
            bool kq = true;
            try
            {
            Cheque cq = new Cheque();
            cq.Money = money;
            cq.DateReceiver = Convert.ToDateTime(Date.ToString("yyyy-MM-dd"));
            cq.DateCreate = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd"));
            cq.NameReceiver = name;
            cq.Address = Address;
            cq.IdAccount = idaccount;
            cq.IdUsername = idusername;
            olbk.Cheques.InsertOnSubmit(cq);
            olbk.SubmitChanges();
            }
            catch
            {
            kq=false;
            }
            return kq;
        
        
        }
        public List<Cheque> ListCheque(int idusername, out int totalCheque)
        {
            totalCheque = olbk.Cheques.Where(ac => ac.IdUsername==idusername).Count();
            return olbk.Cheques.Where(ac => ac.IdUsername == idusername).ToList();
        }
        public bool Delete(int id)
        {
            bool kq=true;
            Cheque cq = olbk.Cheques.Where(a => a.IdCheque == id).First();
            DateTime now = DateTime.Now;
            if (cq.DateReceiver<now)
                return false;
            else
            {
                olbk.Cheques.DeleteOnSubmit(cq);
                olbk.SubmitChanges();
            }
            return kq;
        
        }
        public bool ChangeCheque(int id,string name, string address)
        {
            bool kq = true;
            try
            {
                Cheque c = olbk.Cheques.Where(a => a.IdCheque == id).First();
                c.NameReceiver = name;
                c.Address = address;
                olbk.SubmitChanges();
            }
            catch
            {
                kq = false;
            
            }
            return kq;
        
        }
        public Cheque information(int id)
        {
            return olbk.Cheques.Where(a => a.IdCheque == id).First();
        }
    }
}
